package com.bjpowernode.controller;

import com.bjpowernode.entity.TTran;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.query.TranQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.TranService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TranController {

    @Resource
    private TranService tranService;

    @PostMapping(value = "/api/tran")
    public R createTran(@Valid TranQuery tranQuery) {
        boolean create = tranService.createTran(tranQuery);
        return create ? R.OK() : R.FAIL();
    }

    /**
     * 分页查询交易
     *
     * @param current
     * @return
     */
    @GetMapping(value = "/api/trans")
    public R tranPage(@RequestParam(value = "current", required = false) Integer current) {
        // required = false 表示current参数可以传，也可以不传，也就是current参数可以是空的；
        // required = true 表示current参数必须传，不传会报错；
        if (current == null) {
            current = 1;
        }
        PageInfo<TTran> pageInfo = tranService.getTranPage(current);
        return R.OK(pageInfo);
    }
}
